package com.zhj.util;

import org.springframework.lang.Nullable;

import java.text.DecimalFormat;
import java.util.concurrent.ThreadLocalRandom;

/**
 * <p>Company: 成都返空汇网络技术有限公司</p>
 * <p>Description: 地理位置工具类 </p>
 *
 * @author wanghao
 * @version 1.0.0
 * @email "mailto:wanghao@fkhwl.com"
 * @date 2021.08.03 18:54
 * @since 1.1.0
 */
public class LocationUtils {

    /** DECIMALFORMAT */
    private static final DecimalFormat DECIMALFORMAT = new DecimalFormat("0.00000#");

    /** randomFactor */
    private static final double RANDOM_FACTOR = 0.00050;

    /** RANGE_FACTORY */
    private static final double RANGE_FACTORY = 0.00030;


    /**
     * Gets random point *
     *
     * @param point point
     * @return the random point
     * @since 1.1.0
     */
    public static String getRandomPoint(String point) {
        // point端点在子设备的正负20
        try {
            String[] points = point.split(",");
            for (int i = 0; i < points.length; i++) {
                String ltStr = String.format("%s.%s", subBefore(points[i], "."),
                                             subAfter(points[i], ".").substring(0, 5));
                //获取到point点位的 0.00045，获取小数点后5位
                double lt = Double.parseDouble(ltStr);
                double max = Double.parseDouble(String.format("%.5f", lt + RANDOM_FACTOR));
                double min = Double.parseDouble(String.format("%.5f", lt - RANDOM_FACTOR));
                //获取到当前point的取值范围
                double maxMin = Double.parseDouble(String.format("%.5f", max - RANGE_FACTORY));
                double minMax = Double.parseDouble(String.format("%.5f", min + RANGE_FACTORY));
                double random;
                boolean randomBoolean;
                do {
                    random = Double.parseDouble(String.format("%.5f", min + (ThreadLocalRandom.current().nextDouble() * (max - min))));
                    //最大值范围的最小值：例如 point点为  40 max：+50 为90  min：-10，随机数的范围需要在 60-90之间 以及 -10 - 20之间
                    randomBoolean = !((random >= maxMin && random <= max) || (random >= min && random <= minMax));
                } while (randomBoolean);
                String randomStr = DECIMALFORMAT.format(random);
                points[i] = String.format("%s%s", randomStr, sub(points[i], ltStr.length(), points[i].length()));
            }
            return String.format("%s,%s", points[0], points[1]);
        } catch (Exception e) {
            System.out.println(e);
        }
        return point;
    }

    public static String sub(CharSequence str, int fromIndex, int toIndex) {
        if (isEmpty(str)) {
            return "";
        } else {
            int len = str.length();
            if (fromIndex < 0) {
                fromIndex += len;
                if (fromIndex < 0) {
                    fromIndex = 0;
                }
            } else if (fromIndex > len) {
                fromIndex = len;
            }

            if (toIndex < 0) {
                toIndex += len;
                if (toIndex < 0) {
                    toIndex = len;
                }
            } else if (toIndex > len) {
                toIndex = len;
            }

            if (toIndex < fromIndex) {
                int tmp = fromIndex;
                fromIndex = toIndex;
                toIndex = tmp;
            }

            return fromIndex == toIndex ? "" : str.toString().substring(fromIndex, toIndex);
        }
    }

    public static boolean isEmpty(@Nullable Object str) {
        return str == null || "".equals(str);
    }

    public static String subBefore(CharSequence string, CharSequence separator, boolean isLastSeparator) {
        if (!isEmpty(string) && separator != null) {
            String str = string.toString();
            String sep = separator.toString();
            if (sep.isEmpty()) {
                return "";
            } else {
                int pos = isLastSeparator ? str.lastIndexOf(sep) : str.indexOf(sep);
                return pos == -1 ? str : str.substring(0, pos);
            }
        } else {
            return null == string ? null : string.toString();
        }
    }

    public static String subBefore(CharSequence string, CharSequence separator) {
        return subBefore(string, separator, true);
    }

    @Nullable
    public static String subAfter(CharSequence string, CharSequence separator, boolean isLastSeparator) {
        if (isEmpty(string)) {
            return null == string ? null : string.toString();
        } else if (separator == null) {
            return "";
        } else {
            String str = string.toString();
            String sep = separator.toString();
            int pos = isLastSeparator ? str.lastIndexOf(sep) : str.indexOf(sep);
            return pos == -1 ? "" : str.substring(pos + separator.length());
        }
    }

    public static String subAfter(CharSequence string, CharSequence separator) {
        return subAfter(string, separator, false);
    }

}
